from django.shortcuts import *
from django.views import View
from django.http import JsonResponse, HttpResponse
from .models import *


# Crefrate your views here.
class LoginView(View):

    def get(self, request):

        return render(request, 'login.html')

    def post(self, request):
        name = request.POST.get('uname')
        pwd = request.POST.get('pwd')
        # select * from user where username = name and password = pwd
        user = User.objects.filter(username=name, password=pwd).first()

        request.session["username"] = user.username

        return render(request, 'welcome.html')








class ZhuceView(View):
    def get(self, request):
        print("qqqqqqqqqqqq")
        return render(request, 'zhuce.html')

    def post(self, request):
        name = request.POST.get('uname')
        pwd = request.POST.get('pwd')
        print("bbbbbbbbb")
        user = User.objects.filter(username=name)
        if user:
            return HttpResponse('用户已存在')
        else:
            new = User(username=name, password=pwd)
            new.save()

            print("aaaaaaaaaaaaaa")
        return redirect(reverse('login'))


class AddView(View):
    def get(self, request):
        Tall = NewsType.objects.all()
        return render(request, 'add.html',{'Talls':Tall})

    def post(self, request):
        title = request.POST.get('title')
        author = request.POST.get('author')
        type = request.POST.get('type')
        ns = request.POST.get('NStatus')
        content = request.POST.get('content')
        print(type)
        adds = NewsInfo(nTitle=title, nAuthor=author, nContent=content, NStatus=ns, tid_id=type)
        adds.save()
        return redirect(reverse('all'))


def all(request):
    alls = NewsInfo.objects.all()
    return render(request, 'all.html', {'alls': alls})


def delete(request, id):
    de = NewsInfo.objects.get(pk=id)
    de.delete()
    return redirect(reverse('all'))


class UpdateView(View):
    def get(self, request, id, sid):
        alls = NewsInfo.objects.get(pk=id)
        alla = NewsType.objects.all()
        return render(request, 'update.html', {'alls': alls,'alla':alla})

    def post(self, request, sid, id):
        title = request.POST.get('title')
        author = request.POST.get('author')
        type = request.POST.get('type')
        ns = request.POST.get('NStatus')
        content = request.POST.get('content')
        a = NewsType.objects.filter(id=sid).first()
        alls = NewsInfo.objects.get(pk=id)
        if title== '':
            pass
        else:
            alls.nTitle = title

        if author == '':
            pass
        else:
            alls.nAuthor = author

        if type == '':
            pass
        else:
            if a:
                alls.tid_id = a.id

            else:
                b = NewsType(tname=type)
                b.save()
                alls.tid_id = b.id

        if ns == "":
            pass
        else:
            alls.NStatus = ns

        if content == '':
            pass
        else:
            alls.nContent = content


        alls.save()
        return redirect(reverse('all'))


def only(request):
    user = request.GET.get('user')
    users = User.objects.filter(username=user).first()
    if users:
        return JsonResponse({'flag': 1})
    else:
        return JsonResponse({'flag': 0})


def deleteall(request):
    if request.method == 'POST':

        alls = request.POST.getlist('list')


        for all in alls:
            if all != '':
                a = NewsInfo.objects.get(id=all)
                print(a)
                a.delete()
        return redirect(reverse('all'))
    return redirect(reverse('all'))


def selects(request):
    if request.method =='POST':
        l = request.POST.get('l')
        s = NewsInfo.objects.filter(tid_id=l)
        list = []
        for i in s:
            data = {
                'id':i.id,
                'title':i.nTitle,
                'author':i.nAuthor,
                'content':i.nContent,
                'time':i.nPubDateTime,
                'ns':i.NStatus,
                'typp':i.tid.tname
            }
            list.append(data)
            # print(list)
            # print(JsonResponse(list,safe=False))

        return JsonResponse(list,safe=False)
